package cli;

import java.sql.Timestamp;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

import org.jdom2.Element;

public class Reservation {
	int ref_evt;
	int  ref_client;
	String nom;
	String prenom;
	int age;
	String reduction;

	public Reservation(int ref_evt, int ref_client, String nom, String prenom, int age, String reduction,String file) throws R1Exception, R2Exception, R3Exception, ParseException {

		Timestamp stamp = new Timestamp(System.currentTimeMillis());
		Date date = new Date(stamp.getTime());
 		XML xmldoc = new XML();
		
		Element rootElement = xmldoc.getRootofFile(file);
		
		this.ref_evt = ref_evt;
		this.ref_client = ref_client;
		this.nom = nom;
		this.prenom = prenom;
		this.age = age;
		this.reduction = reduction;

		if (!xmldoc.checkRef(rootElement, String.valueOf(this.ref_evt)))
			throw new R1Exception("Erreur R1 : Réference de l'évennement inexistante");
		else if (date.after(new SimpleDateFormat("dd/MM/yy").parse(xmldoc.getDatefromRef(rootElement, String.valueOf(this.ref_evt)))))
			throw new R2Exception("Erreur R2 : Date incorrecte");
		else if(!xmldoc.enoughPlaces(rootElement, this.ref_evt))
			throw new R3Exception("Erreur R3 : Plus de places disponibles");
		else{
			this.ref_evt = ref_evt;
			this.ref_client = ref_client;
			this.nom = nom;
			this.prenom = prenom;
			this.age = age;
			this.reduction = reduction;
		}
	}
}
